﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.ViewModels/AlarmRecordViewModel
* 类       名: AlarmRecordViewModel
* 功      能:  TD
* 时      间:  2023/9/6 14:58:45
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using OfficeOpenXml;
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using SiliconSelect.DAL;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SiliconSelect.ViewModels
{
    public class AlarmRecordViewModel : NotifyBase
    {
        public AlarmRecordViewModel()
        {
            AlarmRecordList = new ObservableCollection<AlarmModel>();
            RefreshData();
            FlushAlarmCommand = new Command(DoFlushAlarmTask);
            ClearAlarmCommand = new Command(DoClearAlarmTask);
            QueryAlarmCommand = new Command(DoQueryAlarmTask);
        }



        public Command FlushAlarmCommand { get; private set; }
        public Command ClearAlarmCommand { get; private set; }
        public Command QueryAlarmCommand { get; private set; }

        /// <summary>
        /// 刷新界面
        /// </summary>
        /// <param name="obj"></param>
        private void DoFlushAlarmTask(object obj)
        {
            RefreshData();
        }

        /// <summary>
        /// 清空报警
        /// </summary>
        /// <param name="obj"></param>
        private void DoClearAlarmTask(object obj)
        {
            LocalDataAccess.DeleteTableByName(TableNames.AlarmRecordTb);
            RefreshData();
        }

        /// <summary>
        /// 查询报警
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DoQueryAlarmTask(object obj)
        {
            RefreshData();
        }


        private string? _queryVal;

        public string? QueryVal
        {
            get { return _queryVal; }
            set
            {
                SetProperty(ref _queryVal, value);
                if (value != "")
                {
                    IsQueryByDefault = false;
                }
            }
        }

        /// <summary>
        /// 报警记录列表
        /// </summary>
        private ObservableCollection<AlarmModel> _alarmRecordList = null!;

        public ObservableCollection<AlarmModel> AlarmRecordList
        {
            get { return _alarmRecordList; }
            set { SetProperty(ref _alarmRecordList, value); }
        }


        public bool IsQueryByDefault = true;

        private void RefreshData()
        {
            if (IsQueryByDefault)
            {
                DataResult<List<AlarmRecordEntity>> result = LocalDataAccess.GetAlarmRecord();
                AlarmRecordList.Clear();
                if (result.State)
                {
                    foreach (AlarmRecordEntity alarm in result.Data!)
                    {
                        AlarmModel model = new AlarmModel()
                        {
                            Id = alarm.Id,
                            AlarmCode = alarm.AlarmCode,
                            Content = alarm.Content,
                            SolutionHelp = alarm.SolutionHelp,
                            CreateTime = alarm.CreateTime,
                        };
                        AlarmRecordList.Add(model);
                    }
                }
                else
                {
                    // 数据读取失败
                    LogHelper.WriteLog(LogType.Warn, result.Message);
                }
            }
            else
            {
                DataResult<List<AlarmRecordEntity>> result = LocalDataAccess.GetAlarmRecord(queryFeild: "Type", queryVal: QueryVal!);
                AlarmRecordList.Clear();
                if (result.State)
                {
                    foreach (AlarmRecordEntity alarm in result.Data!)
                    {
                        AlarmModel model = new AlarmModel()
                        {
                            Id = alarm.Id,
                            AlarmCode = alarm.AlarmCode,
                            Content = alarm.Content,
                            SolutionHelp = alarm.SolutionHelp,
                            CreateTime = alarm.CreateTime,
                        };
                        AlarmRecordList.Add(model);
                    }
                }
                else
                {
                    // 数据读取失败
                    LogHelper.WriteLog(LogType.Warn, result.Message);
                }
            }
        }
    }
}
